package com.atguigu.spring.pojo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class aopProxy {
    @Pointcut("execution(* com.atguigu.spring.pojo.CalculatorImpl.*(..))")
    public void getPointCut(){}
    //前置通知
    @Before("getPointCut()")
    public void beforeAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        System.out.println("beforeAdviceMethod-->前置通知,方法为:"+signature.getName());
    }
    //后置通知
    @After("getPointCut()")
    public void afterAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        System.out.println("afterAdviceMethod-->后置通知,方法为:"+signature.getName());
    }
    //返回通知
    @AfterReturning(value = "getPointCut()",returning = "result")
    public void afterReturningMethod(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        System.out.println("afterReturningMethod-->返回通知，方法为:"+signature.getName()+",结果为:"+result);
    }
    //异常通知
    @AfterThrowing(value = "getPointCut()",throwing = "e")
    public void afterThrowingMethod(JoinPoint joinPoint,Throwable e){
        Signature signature = joinPoint.getSignature();
        System.out.println("afterThrowingMethod-->异常通知"+signature.getName()+",异常为:"+e);
    }
    //环绕通知
    @Around("getPointCut()")
    public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
        Object proceed = null;
        try {
            System.out.println("环绕通知,前置通知");
            proceed = joinPoint.proceed();
            System.out.println("环绕通知,返回通知");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知,异常通知");
        } finally {
            System.out.println("环绕通知,后置通知");
        }
        return proceed;
    }
}
